﻿using Microsoft.VisualBasic;
using SqlSugar;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace LD
{
    public partial class FrmPartNo : Form
    {
        private ToolTip toolTip = new ToolTip();
        public FrmPartNo()
        {
            InitializeComponent();
            SetToolTip();
            UploadData();
            UserHelper.Instance.UserChangeEvent += UserChangeEvent;
            this.VisibleChanged += FrmPartNo_VisibleChanged;
        }

        private void FrmPartNo_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                this.sw_add.Checked = false;
                this.sw_copy.Checked = false;
                this.sw_edit.Checked = false;
                this.sw_delete.Checked = false;
                this.sw_load.Checked = false;
            }
        }

        private void Instance_StatusChangeEvent(string status)
        {
            if (!this.IsDisposed)
            {
                this.Invoke(new Action(() =>
                {
                    switch (status)
                    {
                        case "Start":
                            panel_top.Enabled = false;
                            break;
                        case "Stop":
                            panel_top.Enabled = true;
                            break;
                        default:
                            break;
                    }
                }));
            }
        }

        private void UserChangeEvent(UserModel user)
        {
            if (user.UserType == UserType.操作工)
                panel_top.Visible = false;
            else
                panel_top.Visible = true;
        }

        private void SetToolTip()
        {
            toolTip.SetToolTip(this.sw_add, "新增");
            toolTip.SetToolTip(this.sw_copy, "复制");
            toolTip.SetToolTip(this.sw_edit, "重命名");
            toolTip.SetToolTip(this.sw_delete, "删除");
            toolTip.SetToolTip(this.sw_load, "加载");
        }
        private void UploadData()
        {
            this.dgv_data.Rows.Clear();
            var parts = PartNoHelper.Instance.GetPartNoList;
            foreach (var item in parts)
            {
                this.dgv_data.Rows.Add(item);
            }
            this.dgv_data.ClearSelection();

            if (PartNoHelper.Instance.CurrentPartNo == null) return;

            foreach (DataGridViewRow item in this.dgv_data.Rows)
            {
                if (PartNoHelper.Instance.CurrentPartNo.Name == item.Cells["名称"].Value.ToString())
                {
                    item.Selected = true;
                    this.dgv_data.CurrentCell = item.Cells[0];
                    break;
                }
            }
        }
        private void sw_add_Click(object sender, EventArgs e)
        {
            var result = Interaction.InputBox("请输入配方名称", "新建配方", "", -1, -1);
            if (result.Contains(" "))
            {
                MessageBox.Show("录入内容不允许有空格");
                this.TopMost = true;
                return;
            }
            var isExist = PartNoHelper.Instance.GetPartNoList
                            .Where(p => p == result).FirstOrDefault();
            if (isExist != null)
            {
                MessageBox.Show("配方已存在");
                this.TopMost = true;
                return;
            }
            PartNoHelper.Instance.Add(result);
            UploadData();
        }

        private void sw_load_Click(object sender, EventArgs e)
        {
            var row = this.dgv_data.SelectedRows;
            if (row.Count > 0)
            {
                string sourceName = row[0].Cells[0].Value.ToString();
                PartNoHelper.Instance.Load(sourceName);

                //SciHelper.Instance.ChangePartNo(PartNoHelper.Instance.CurrentPartNo.CCDName);
            }
        }

        private void sw_copy_Click(object sender, EventArgs e)
        {
            var row = this.dgv_data.SelectedRows;
            if (row.Count == 0)
                return;

            FrmPartNoEdit frmPartNoEdit = new FrmPartNoEdit();
            frmPartNoEdit.ShowDialog();
            if (frmPartNoEdit.Data == null) return;

            var isExist = PartNoHelper.Instance.GetPartNoList
                            .Where(p => p == frmPartNoEdit.Data.Name).FirstOrDefault();
            if (string.IsNullOrEmpty(frmPartNoEdit.Data.Name))
            {
                return;
            }
            if (isExist != null)
            {
                MessageBox.Show("配方已存在");
                this.TopMost = true;
                return;
            }

            //复制配方
            string sourceName = row[0].Cells[0].Value.ToString();
            PartNoHelper.Instance.Add(frmPartNoEdit.Data.Name);
            PartNoHelper.Instance.Copy(sourceName, frmPartNoEdit.Data);
            UploadData();
        }
        /// <summary>
        /// 重命名
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void sw_edit_Click(object sender, EventArgs e)
        {
            var row = this.dgv_data.SelectedRows;
            if (row.Count == 0)
                return;
            string sourceName = row[0].Cells[0].Value.ToString();

            if (PartNoHelper.Instance.CurrentPartNo.Name == sourceName)
            {
                MessageBox.Show("使用中不允许变更");
                this.TopMost = true;
                return;
            }
            FrmPartNoEdit partNoEdit = new FrmPartNoEdit(sourceName);
            partNoEdit.ShowDialog();
            if (partNoEdit.Data == null) return;
            var isExist = PartNoHelper.Instance.GetPartNoList
                           .Where(p => p == partNoEdit.Data.Name).FirstOrDefault();
            if (isExist != null && isExist != sourceName)
            {
                MessageBox.Show("配方已存在");
                this.TopMost = true;
                return;
            }

            for (int i = 0; i < PartNoHelper.Instance.GetPartNoList.Count; i++)
            {
                if (PartNoHelper.Instance.GetPartNoList[i] == sourceName)
                {
                    PartNoHelper.Instance.GetPartNoList[i] = partNoEdit.Data.Name;
                    break;
                }
            }
            PartNoHelper.Instance.Save();
            PartNoHelper.Instance.Rename(sourceName, partNoEdit.Data);
            UploadData();
        }

        private void sw_delete_Click(object sender, EventArgs e)
        {
            var row = this.dgv_data.SelectedRows;
            if (row.Count == 0)
                return;
            string sourceName = row[0].Cells[0].Value.ToString();
            if (PartNoHelper.Instance.CurrentPartNo.Name == sourceName)
            {
                MessageBox.Show("使用中不允许删除");
                this.TopMost = true;
                return;
            }

            PartNoHelper.Instance.Delete(sourceName);
            UploadData();
        }
    }
}
